package com.xiaoq.flowrate.sitesInterface.qingyan;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaoq.coms.util.JsonUtil;
import com.xiaoq.flowrate.sitesInterface.SitesFlowrate;
import com.xiaoq.sites.vo.TouristStaticsVo;
import com.xiaoq.web.DefaultMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
/**
 * 考虑还是使用反射比较靠谱
 * @author wang zhihao
 *
 */
@SuppressWarnings("deprecation")
public class QingYanFlowrate extends SitesFlowrate {
	
	private static final Logger LOG = LoggerFactory.getLogger(QingYanFlowrate.class);

	@Override
	public TouristStaticsVo getSitesFlowrate(String url, String param,Long capacity) {
		QingYanParamVo qingyanPara = JsonUtil.asObject(param, QingYanParamVo.class);
		String paraUrl = url+"?"+qingyanPara.getString();
		LOG.info("青岩古镇获取实时人流量URL:"+paraUrl);
		HttpGet request = new HttpGet(paraUrl);
		//设置timeout
		HttpParams my_httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(my_httpParams, 2000);
		HttpConnectionParams.setSoTimeout(my_httpParams, 2000);

		HttpClient httpClient = new DefaultHttpClient(my_httpParams);

		HttpResponse response;
		TouristStaticsVo toutistStatics = new TouristStaticsVo();;
		try {
			response = httpClient.execute(request);
			HttpEntity entity =response.getEntity();
			String body = EntityUtils.toString(entity, "utf-8");
			ObjectMapper objectMapper = new DefaultMapper();
			QingYanResponseVo qingyanResponse = objectMapper.readValue(body, QingYanResponseVo.class);

			if(qingyanResponse!=null && qingyanResponse.getResults() != null){
				Long checkNum = qingyanResponse.getResults().checkNum;
				if(checkNum!=null && checkNum!=0 && capacity!=0L){
					toutistStatics.setCount(checkNum);
					Double rate =  (checkNum/((double)capacity));
					if(rate<=0.2){
						toutistStatics.setCongestionIdx(1);
					}else if(rate>0.2&&rate<=0.7){
						toutistStatics.setCongestionIdx(2);
					}else if(rate>0.7&&rate<=0.9){
						toutistStatics.setCongestionIdx(3);
					}else{
						toutistStatics.setCongestionIdx(4);
					}
				}else{
					toutistStatics.setCount(-1L);
					toutistStatics.setCongestionIdx(-1); // TODO 这个拥堵程度需要处理一下
				}
			}else {
				toutistStatics.setCount(-1L);
				toutistStatics.setCongestionIdx(-1); // TODO 这个拥堵程度需要处理一下
			}

			return toutistStatics;
		} catch (ClientProtocolException e) {
			LOG.error("客户端端口异常", e);

			//TODO 调用接口异常返回-2待处理
			
		} catch (IOException e) {
			LOG.error("IO流异常", e);
		}catch (Exception e){
			LOG.error(e.getMessage(), e);
		}finally {
			//释放资源
			request.releaseConnection();
		}

		toutistStatics.setCount(-1L);
		toutistStatics.setCongestionIdx(-1); // TODO 这个拥堵程度需要处理一下
		return toutistStatics;
	}

}
